<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 冠亚军排名，奖牌榜排名（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p> 2012伦敦奥运会即将到来&#xff0c;大家都非常关注奖牌榜的情况&#xff0c;现在我们假设奖牌榜的排名规则如下:</p> 
<ol><li>首先gold medal数量多的排在前面</li><li>其次silver medal数量多的排在前面</li><li>然后bronze medal数量多的排在前面</li><li>若以上三个条件仍无法区分名次&#xff0c;则以国家名称的字典顺序排定。</li></ol> 
<p>我们假设国家名称不超过二十个字符&#xff0c;各类奖牌数不超过100&#xff0c;且大于0.</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行输入一个整数N&#xff08;0&lt;N&lt;21&#xff09;&#xff0c;代表国家数量&#xff0c;</p> 
<p>然后接下来的N行&#xff0c;每行包含&#xff1a;</p> 
<p>一个字符串Name表示各个国家的名称和三个整数Gi,Si,Bi表示每个获得的gold medal,silver medal,bronze medal的数量&#xff0c;以空格隔开&#xff0c;如(China 51 20 21),</p> 
<p>具体见样例输入。</p> 
<blockquote> 
 <p>5<br /> China 32 28 34<br /> England 12 34 22<br /> France 23 33 2<br /> Japan 12 34 25<br /> Rusia 23 43 0</p> 
</blockquote> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出奖牌榜的依次顺序&#xff0c;只输出国家名称&#xff0c;各占一行&#xff0c;具体见样例输出。</p> 
<blockquote> 
 <p>China<br /> Rusia<br /> France<br /> Japan<br /> England</p> 
</blockquote> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">5<br /> China 32 28 34<br /> England 12 34 22<br /> France 23 33 2<br /> Japan 12 34 25<br /> Rusia 23 43 0</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">China<br /> Rusia<br /> France<br /> Japan<br /> England</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>主要考察数组排序&#xff08;也可以集合排序&#xff09;</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
let n;
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 1) {
    n &#61; lines[0] - 0;
  }

  if (n &amp;&amp; lines.length &#61;&#61;&#61; n &#43; 1) {
    lines.shift();

    lines
      .map((line) &#61;&gt; {
        const arr &#61; line.split(&#34; &#34;);
        return {
          name: arr[0],
          gold: arr[1] - 0,
          silver: arr[2] - 0,
          bronze: arr[3] - 0,
        };
      })
      .sort((a, b) &#61;&gt; {
        return b.gold !&#61;&#61; a.gold
          ? b.gold - a.gold
          : b.silver !&#61;&#61; a.silver
          ? b.silver - a.silver
          : b.bronze !&#61;&#61; a.bronze
          ? b.bronze - a.bronze
          : a.name &#61;&#61; b.name
          ? 0
          : a.name &gt; b.name
          ? 1
          : -1;
      })
      .forEach((country) &#61;&gt; {
        console.log(country.name);
      });

    lines.length &#61; 0;
  }
});
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  // 输入获取
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();

    Country[] countries &#61; new Country[n];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      Country c &#61; new Country(sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());
      countries[i] &#61; c;
    }

    getResult(countries);
  }

  // 算法入口
  public static void getResult(Country[] countries) {
    Arrays.sort(
        countries,
        (a, b) -&gt;
            b.gold !&#61; a.gold
                ? b.gold - a.gold
                : b.silver !&#61; a.silver
                    ? b.silver - a.silver
                    : b.bronze !&#61; a.bronze ? b.bronze - a.bronze : a.name.compareTo(b.name));

    for (Country country : countries) {
      System.out.println(country.name);
    }
  }
}

class Country {
  String name;
  int gold;
  int silver;
  int bronze;

  public Country(String name, int gold, int silver, int bronze) {
    this.name &#61; name;
    this.gold &#61; gold;
    this.silver &#61; silver;
    this.bronze &#61; bronze;
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
countries &#61; []

for i in range(n):
    name, gold, silver, bronze &#61; input().split()
    countries.append([int(gold), int(silver), int(bronze), name])


# 算法入口
def getResult():
    countries.sort(key&#61;lambda x: (-x[0], -x[1], -x[2], x[3]))

    for c in countries:
        print(c[3])


# 算法调用
getResult()
</code></pre> 
<p> </p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

typedef struct {
    char name[20];
    int gold;
    int silver;
    int bronze;
} Country;

int cmp(const void* a, const void* b) {
    Country* A &#61; (Country*) a;
    Country* B &#61; (Country*) b;

    if(A-&gt;gold !&#61; B-&gt;gold) {
        return B-&gt;gold - A-&gt;gold;
    } else if(A-&gt;silver !&#61; B-&gt;silver) {
        return B-&gt;silver - A-&gt;silver;
    } else if(A-&gt;bronze !&#61; B-&gt;bronze) {
        return B-&gt;bronze - A-&gt;bronze;
    } else {
        return strcmp(A-&gt;name, B-&gt;name);
    }
}

int main() {
    int n;
    scanf(&#34;%d&#34;, &amp;n);

    Country countries[n];
    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        scanf(&#34;%s&#34;, countries[i].name);
        scanf(&#34;%d&#34;, &amp;countries[i].gold);
        scanf(&#34;%d&#34;, &amp;countries[i].silver);
        scanf(&#34;%d&#34;, &amp;countries[i].bronze);
    }

    qsort(countries, n, sizeof(Country), cmp);

    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        puts(countries[i].name);
    }

    return 0;
}</code></pre> 
<p> </p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>